package cn.edu.tsinghua.smxq.mobilecity.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.tsinghua.smxq.mobilecity.po.PayRecord;
import cn.edu.tsinghua.smxq.mobilecity.utils.DBUtils;

public class PayRecordDAO {
	
	public boolean create(PayRecord payRecord) throws SQLException {
		boolean success = false;
		Connection conn = DBUtils.Connect();
		PreparedStatement pstmt = conn
				.prepareStatement("INSERT INTO mobilecity.payrecord(userid,recordid,transerialno,amount,merfeeamt,curtype,merid,meracct,customcardnum,remark,time) values(?,?,?,?,?,?,?,?,?,?,?)");
		pstmt.setInt(1, payRecord.getUserId());
		pstmt.setInt(2, payRecord.getRecordId());
		pstmt.setString(3, payRecord.getTranSerialNo());
		pstmt.setFloat(4, payRecord.getAmount());
		pstmt.setFloat(5, payRecord.getMerFeeAmt());
		pstmt.setInt(6, payRecord.getCurType());
		pstmt.setInt(7, payRecord.getMerId());
		pstmt.setString(8, payRecord.getMerAcct());
		pstmt.setString(9, payRecord.getCustomCardNum());
		pstmt.setInt(10, payRecord.getRemark());
		pstmt.setTimestamp(11, payRecord.getTime());
	
		if (pstmt.executeUpdate() == 1) {
			success = true;
		}
		pstmt.close();
		conn.close();
		return success;
	}
	
	public PayRecord getByOtherInfo(int recordid,int remark)throws SQLException{
		PayRecord payRecord = null;
		Connection conn = DBUtils.Connect();
		ResultSet rs = null;
		PreparedStatement pstmt = conn
				.prepareStatement("SELECT * FROM mobilecity.payrecord WHERE recordid=? and remark=?");
		pstmt.setInt(1, recordid);
		pstmt.setInt(2, remark);
		rs = pstmt.executeQuery();
		if (rs.next()) {
			payRecord = new PayRecord();
			payRecord.setId(rs.getInt("id"));
			payRecord.setUserId(rs.getInt("userid"));
			payRecord.setRecordId(rs.getInt("recordid"));
			payRecord.setTranSerialNo(rs.getString("transerialno"));
			payRecord.setAmount(rs.getFloat("amount"));
			payRecord.setMerFeeAmt(rs.getFloat("merFeeAmt"));
			payRecord.setCurType(rs.getInt("curtype"));
			payRecord.setMerId(rs.getInt("merid"));
			payRecord.setMerAcct(rs.getString("merfeeamt"));
			payRecord.setCustomCardNum(rs.getString("customcardnum"));
			payRecord.setRemark(rs.getInt("remark"));
			payRecord.setTime(rs.getTimestamp("time"));
		}
		rs.close();
		pstmt.close();
		conn.close();
		return payRecord;
	}
}
